<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/3/14
 * Time: 13:08
 */

namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;
use app\index\validate\Valid;

class Schdbf extends Common
{
    /**
     * 我的市场活动拜访
     */
    public function mymarketactionbf(){
        $cxbh = $this->request->param('cxbh');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $sqlx = $this->request->param('sqlx');
        $wdbm = $this->request->param('wdbm');
        $wdmc = $this->request->param('wdmc');
        $hbrxz = $this->request->param('hbrxz');
        $hbrxm = $this->request->param('hbrxm');
        $hbrbm = $this->request->param('hbrbm');
        $sfjhhx = $this->request->param('sfjhhx');
        $fylx = $this->request->param('fylx');
        $shjg = $this->request->param('shjg');
        $this->assign('cxbh',$cxbh);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('sqlx',$sqlx);
        $this->assign('wdbm',$wdbm);
        $this->assign('wdmc',$wdmc);
        $this->assign('hbrxz',$hbrxz);
        $this->assign('hbrxm',$hbrxm);
        $this->assign('hbrbm',$hbrbm);
        $this->assign('sfjhhx',$sfjhhx);
        $this->assign('fylx',$fylx);
        $this->assign('shjg',$shjg);
        $where = [];
        $where['hbr'] = session('employee_id');
        if($cxbh){
            $where['cxsqbh'] = ['like','%'.$cxbh.'%'];
        }
        if($start){
            $where['kssj'] = ['>=',$start];
        }
        if($end){
            $where['jzsj'] = ['<=',$end];
        }
        if($sqlx){
            $where['sqlx'] = $sqlx;
        }
        if($wdbm){
            $where['wdbm'] = ['like','%'.$wdbm.'%'];
        }
        if($wdmc){
            $where['wdmc'] = ['like','%'.$wdmc.'%'];
        }
        if($hbrxz){
            $where['hbr_xz'] = $hbrxz;
        }
        if($hbrxm){
            $where['hbr_xm'] = ['like','%'.$hbrxm.'%'];
        }
        if($hbrbm){
            $where['hbr_bm'] = ['like','%'.$hbrbm.'%'];
        }
        if($sfjhhx){
            $where['sfjyhx'] = $sfjhhx;
        }
        if($fylx){
            $where['fylx'] = ['like','%'.$fylx.'%'];
        }
        if($shjg){
            $where['shzt'] = ['like','%'.$shjg.'%'];
        }
        $cnt = db('yd_schdbf')->where($where)->count();
        $data = db('yd_schdbf')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);

        //汇报人性质
        $data1 = db('yd_hbrxz')->select();
        $this->assign('data1',$data1);
        //申请类型
        $data2 = db('yd_sqlx')->select();
        $this->assign('data2',$data2);
        //审核结果
        $data3 = db('yd_shjg')->select();
        $this->assign('data3',$data3);
        return $this->fetch();
    }
    /**
     * 市场活动拜访详情
     */
    public function xiashumarkethdbfdt(){
        $id = $this->request->param('id');
        $data = db('yd_schdbf')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    /**
     * 市场活动拜访审核
     */
    public function xiashumarkethdbfshenhe(){
        $id = $this->request->param('id');
        $data = db('yd_schdbf')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 审核通过
     */
    public function shtg(){
        $id = $this->request->post('id');
        $dpxx = $this->request->post('dpxx');
        $valid = new Valid([
            'dpxx'=>'require'
        ]);
        if (!$valid->check(['dpxx'=>$dpxx])) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$valid->getError()
            ]);die;
        }
        $bool = db('yd_schdbf')->where('id',$id)->update(['dpxx'=>$dpxx,'shzt'=>1]);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"审核通过成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"审核失败"
            ]);die;
        }
    }

    /**
     * 重新整改
     */
    public function cxzg(){
        $id = $this->request->post('id');
        $dpxx = $this->request->post('dpxx');
        $valid = new Valid([
            'dpxx'=>'require'
        ]);
        if (!$valid->check(['dpxx'=>$dpxx])) {
            echo  json_encode([
                "success"=>false,
                "msg"=>$valid->getError()
            ]);die;
        }
        $bool = db('yd_schdbf')->where('id',$id)->update(['dpxx'=>$dpxx,'shzt'=>2]);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"重新整改成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"审核失败"
            ]);die;
        }
    }
    /**
     * 下属市场活动拜访
     */
    public function xiashumarkethdbf(){
        $cxbh = $this->request->param('cxbh');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $sqlx = $this->request->param('sqlx');
        $wdbm = $this->request->param('wdbm');
        $wdmc = $this->request->param('wdmc');
        $hbrxz = $this->request->param('hbrxz');
        $hbrxm = $this->request->param('hbrxm');
        $hbrbm = $this->request->param('hbrbm');
        $sfjhhx = $this->request->param('sfjhhx');
        $fylx = $this->request->param('fylx');
        $shjg = $this->request->param('shjg');
        $this->assign('cxbh',$cxbh);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('sqlx',$sqlx);
        $this->assign('wdbm',$wdbm);
        $this->assign('wdmc',$wdmc);
        $this->assign('hbrxz',$hbrxz);
        $this->assign('hbrxm',$hbrxm);
        $this->assign('hbrbm',$hbrbm);
        $this->assign('sfjhhx',$sfjhhx);
        $this->assign('fylx',$fylx);
        $this->assign('shjg',$shjg);
        $where = [];
        $where['hbr'] = session('employee_id');
        if($cxbh){
            $where['cxsqbh'] = ['like','%'.$cxbh.'%'];
        }
        if($start){
            $where['kssj'] = ['>=',$start];
        }
        if($end){
            $where['jzsj'] = ['<=',$end];
        }
        if($sqlx){
            $where['sqlx'] = $sqlx;
        }
        if($wdbm){
            $where['wdbm'] = ['like','%'.$wdbm.'%'];
        }
        if($wdmc){
            $where['wdmc'] = ['like','%'.$wdmc.'%'];
        }
        if($hbrxz){
            $where['hbr_xz'] = $hbrxz;
        }
        if($hbrxm){
            $where['hbr_xm'] = ['like','%'.$hbrxm.'%'];
        }
        if($hbrbm){
            $where['hbr_bm'] = ['like','%'.$hbrbm.'%'];
        }
        if($sfjhhx){
            $where['sfjyhx'] = $sfjhhx;
        }
        if($fylx){
            $where['fylx'] = ['like','%'.$fylx.'%'];
        }
        if($shjg){
            $where['shzt'] = ['like','%'.$shjg.'%'];
        }
        $cnt = db('yd_schdbf')->where($where)->count();
        $data = db('yd_schdbf')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);

        //汇报人性质
        $data1 = db('yd_hbrxz')->select();
        $this->assign('data1',$data1);
        //申请类型
        $data2 = db('yd_sqlx')->select();
        $this->assign('data2',$data2);
        //审核结果
        $data3 = db('yd_shjg')->select();
        $this->assign('data3',$data3);
        return $this->fetch();
    }
    /**
     * 所属市场活动拜访
     */
    public function tomarkethdbf(){
        $cxbh = $this->request->param('cxbh');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $sqlx = $this->request->param('sqlx');
        $wdbm = $this->request->param('wdbm');
        $wdmc = $this->request->param('wdmc');
        $hbrxz = $this->request->param('hbrxz');
        $hbrxm = $this->request->param('hbrxm');
        $hbrbm = $this->request->param('hbrbm');
        $sfjhhx = $this->request->param('sfjhhx');
        $fylx = $this->request->param('fylx');
        $shjg = $this->request->param('shjg');
        $this->assign('cxbh',$cxbh);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('sqlx',$sqlx);
        $this->assign('wdbm',$wdbm);
        $this->assign('wdmc',$wdmc);
        $this->assign('hbrxz',$hbrxz);
        $this->assign('hbrxm',$hbrxm);
        $this->assign('hbrbm',$hbrbm);
        $this->assign('sfjhhx',$sfjhhx);
        $this->assign('fylx',$fylx);
        $this->assign('shjg',$shjg);
        $where = [];
        $where['hbzt'] = 1;
        if($cxbh){
            $where['cxsqbh'] = ['like','%'.$cxbh.'%'];
        }
        if($start){
            $where['kssj'] = ['>=',$start];
        }
        if($end){
            $where['jzsj'] = ['<=',$end];
        }
        if($sqlx){
            $where['sqlx'] = $sqlx;
        }
        if($wdbm){
            $where['wdbm'] = ['like','%'.$wdbm.'%'];
        }
        if($wdmc){
            $where['wdmc'] = ['like','%'.$wdmc.'%'];
        }
        if($hbrxz){
            $where['hbr_xz'] = $hbrxz;
        }
        if($hbrxm){
            $where['hbr_xm'] = ['like','%'.$hbrxm.'%'];
        }
        if($hbrbm){
            $where['hbr_bm'] = ['like','%'.$hbrbm.'%'];
        }
        if($sfjhhx){
            $where['sfjyhx'] = $sfjhhx;
        }
        if($fylx){
            $where['fylx'] = ['like','%'.$fylx.'%'];
        }
        if($shjg){
            $where['shzt'] = ['like','%'.$shjg.'%'];
        }
        $cnt = db('yd_schdbf')->where($where)->count();
        $data = db('yd_schdbf')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['shzt'] = db('yd_shjg')->where('id',$v['shzt'])->value('title');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);

        //汇报人性质
        $data1 = db('yd_hbrxz')->select();
        $this->assign('data1',$data1);
        //申请类型
        $data2 = db('yd_sqlx')->select();
        $this->assign('data2',$data2);
        //审核结果
        $data3 = db('yd_shjg')->select();
        $this->assign('data3',$data3);
        return $this->fetch();
    }

    /**
     * 所属市场活动拜访--未汇报
     */
    public function tomarkethdbf1(){
        $cxbh = $this->request->param('cxbh');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $sqlx = $this->request->param('sqlx');
        $wdbm = $this->request->param('wdbm');
        $wdmc = $this->request->param('wdmc');
        $hbrxz = $this->request->param('hbrxz');
        $hbrxm = $this->request->param('hbrxm');
        $hbrbm = $this->request->param('hbrbm');
        $sfjhhx = $this->request->param('sfjhhx');
        $fylx = $this->request->param('fylx');
        $shjg = $this->request->param('shjg');
        $this->assign('cxbh',$cxbh);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('sqlx',$sqlx);
        $this->assign('wdbm',$wdbm);
        $this->assign('wdmc',$wdmc);
        $this->assign('hbrxz',$hbrxz);
        $this->assign('hbrxm',$hbrxm);
        $this->assign('hbrbm',$hbrbm);
        $this->assign('sfjhhx',$sfjhhx);
        $this->assign('fylx',$fylx);
        $this->assign('shjg',$shjg);
        $where = [];
        $where['hbzt'] = 0;
        if($cxbh){
            $where['cxsqbh'] = ['like','%'.$cxbh.'%'];
        }
        if($start){
            $where['kssj'] = ['>=',$start];
        }
        if($end){
            $where['jzsj'] = ['<=',$end];
        }
        if($sqlx){
            $where['sqlx'] = $sqlx;
        }
        if($wdbm){
            $where['wdbm'] = ['like','%'.$wdbm.'%'];
        }
        if($wdmc){
            $where['wdmc'] = ['like','%'.$wdmc.'%'];
        }
        if($hbrxz){
            $where['hbr_xz'] = $hbrxz;
        }
        if($hbrxm){
            $where['hbr_xm'] = ['like','%'.$hbrxm.'%'];
        }
        if($hbrbm){
            $where['hbr_bm'] = ['like','%'.$hbrbm.'%'];
        }
        if($sfjhhx){
            $where['sfjyhx'] = $sfjhhx;
        }
        if($fylx){
            $where['fylx'] = ['like','%'.$fylx.'%'];
        }
        if($shjg){
            $where['shzt'] = ['like','%'.$shjg.'%'];
        }
        $cnt = db('yd_schdbf')->where($where)->count();
        $data = db('yd_schdbf')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
//            $v['hbzt'] = db('yd_shjg')->where('id',$v['shzt'])->value('title');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);

        //汇报人性质
        $data1 = db('yd_hbrxz')->select();
        $this->assign('data1',$data1);
        //申请类型
        $data2 = db('yd_sqlx')->select();
        $this->assign('data2',$data2);
        //审核结果
        $data3 = db('yd_shjg')->select();
        $this->assign('data3',$data3);
        return $this->fetch();
    }

    /**
     * 所有市场活动拜访
     */
    public function allmarkethdbf(){
        $cxbh = $this->request->param('cxbh');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $sqlx = $this->request->param('sqlx');
        $wdbm = $this->request->param('wdbm');
        $wdmc = $this->request->param('wdmc');
        $hbrxz = $this->request->param('hbrxz');
        $hbrxm = $this->request->param('hbrxm');
        $hbrbm = $this->request->param('hbrbm');
        $sfjhhx = $this->request->param('sfjhhx');
        $fylx = $this->request->param('fylx');
        $shjg = $this->request->param('shjg');
        $this->assign('cxbh',$cxbh);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('sqlx',$sqlx);
        $this->assign('wdbm',$wdbm);
        $this->assign('wdmc',$wdmc);
        $this->assign('hbrxz',$hbrxz);
        $this->assign('hbrxm',$hbrxm);
        $this->assign('hbrbm',$hbrbm);
        $this->assign('sfjhhx',$sfjhhx);
        $this->assign('fylx',$fylx);
        $this->assign('shjg',$shjg);
        $where = [];
        $where['hbzt'] = 1;
        if($cxbh){
            $where['cxsqbh'] = ['like','%'.$cxbh.'%'];
        }
        if($start){
            $where['kssj'] = ['>=',$start];
        }
        if($end){
            $where['jzsj'] = ['<=',$end];
        }
        if($sqlx){
            $where['sqlx'] = $sqlx;
        }
        if($wdbm){
            $where['wdbm'] = ['like','%'.$wdbm.'%'];
        }
        if($wdmc){
            $where['wdmc'] = ['like','%'.$wdmc.'%'];
        }
        if($hbrxz){
            $where['hbr_xz'] = $hbrxz;
        }
        if($hbrxm){
            $where['hbr_xm'] = ['like','%'.$hbrxm.'%'];
        }
        if($hbrbm){
            $where['hbr_bm'] = ['like','%'.$hbrbm.'%'];
        }
        if($sfjhhx){
            $where['sfjyhx'] = $sfjhhx;
        }
        if($fylx){
            $where['fylx'] = ['like','%'.$fylx.'%'];
        }
        if($shjg){
            $where['shzt'] = ['like','%'.$shjg.'%'];
        }
        $cnt = db('yd_schdbf')->where($where)->count();
        $data = db('yd_schdbf')->where($where)->paginate(10,$cnt);
//        echo db('yd_schdbf')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['shzt'] = db('yd_shjg')->where('id',$v['shzt'])->value('title');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);

        //汇报人性质
        $data1 = db('yd_hbrxz')->select();
        $this->assign('data1',$data1);
        //申请类型
        $data2 = db('yd_sqlx')->select();
        $this->assign('data2',$data2);
        //审核结果
        $data3 = db('yd_shjg')->select();
        $this->assign('data3',$data3);
        return $this->fetch();
    }
    /**
     * 所有市场活动拜访--未汇报
     */
    public function allmarkethdbf1(){
        $cxbh = $this->request->param('cxbh');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $sqlx = $this->request->param('sqlx');
        $wdbm = $this->request->param('wdbm');
        $wdmc = $this->request->param('wdmc');
        $hbrxz = $this->request->param('hbrxz');
        $hbrxm = $this->request->param('hbrxm');
        $hbrbm = $this->request->param('hbrbm');
        $sfjhhx = $this->request->param('sfjhhx');
        $fylx = $this->request->param('fylx');
        $shjg = $this->request->param('shjg');
        $this->assign('cxbh',$cxbh);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('sqlx',$sqlx);
        $this->assign('wdbm',$wdbm);
        $this->assign('wdmc',$wdmc);
        $this->assign('hbrxz',$hbrxz);
        $this->assign('hbrxm',$hbrxm);
        $this->assign('hbrbm',$hbrbm);
        $this->assign('sfjhhx',$sfjhhx);
        $this->assign('fylx',$fylx);
        $this->assign('shjg',$shjg);
        $where = [];
        $where['hbzt'] = 0;
        if($cxbh){
            $where['cxsqbh'] = ['like','%'.$cxbh.'%'];
        }
        if($start){
            $where['kssj'] = ['>=',$start];
        }
        if($end){
            $where['jzsj'] = ['<=',$end];
        }
        if($sqlx){
            $where['sqlx'] = $sqlx;
        }
        if($wdbm){
            $where['wdbm'] = ['like','%'.$wdbm.'%'];
        }
        if($wdmc){
            $where['wdmc'] = ['like','%'.$wdmc.'%'];
        }
        if($hbrxz){
            $where['hbr_xz'] = $hbrxz;
        }
        if($hbrxm){
            $where['hbr_xm'] = ['like','%'.$hbrxm.'%'];
        }
        if($hbrbm){
            $where['hbr_bm'] = ['like','%'.$hbrbm.'%'];
        }
        if($sfjhhx){
            $where['sfjyhx'] = $sfjhhx;
        }
        if($fylx){
            $where['fylx'] = ['like','%'.$fylx.'%'];
        }
        if($shjg){
            $where['shzt'] = ['like','%'.$shjg.'%'];
        }
        $cnt = db('yd_schdbf')->where($where)->count();
        $data = db('yd_schdbf')->where($where)->paginate(10,$cnt);
//        echo db('yd_schdbf')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['shzt'] = db('yd_shjg')->where('id',$v['shzt'])->value('title');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);

        //汇报人性质
        $data1 = db('yd_hbrxz')->select();
        $this->assign('data1',$data1);
        //申请类型
        $data2 = db('yd_sqlx')->select();
        $this->assign('data2',$data2);
        //审核结果
        $data3 = db('yd_shjg')->select();
        $this->assign('data3',$data3);
        return $this->fetch();
    }
}